package cn.iocoder.yudao.module.reclaim.service.kefu;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.reclaim.controller.admin.kefu.vo.AdminKefuPageReqVO;
import cn.iocoder.yudao.module.reclaim.controller.admin.kefu.vo.AdminKefuSaveReqVO;
import cn.iocoder.yudao.module.reclaim.dal.dataobject.kefu.AdminKefuDO;
import cn.iocoder.yudao.module.reclaim.dal.mysql.kefu.AdminKefuMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.reclaim.enums.ErrorCodeConstants.ADMIN_KEFU_NOT_EXISTS;

/**
 * 客服 Service 实现类
 *
 * @author deyaco
 */
@Service
@Validated
public class AdminKefuServiceImpl implements AdminKefuService {

    @Resource
    private AdminKefuMapper adminKefuMapper;

    @Override
    public Long createAdminKefu(AdminKefuSaveReqVO createReqVO) {
        // 插入
        AdminKefuDO adminKefu = BeanUtils.toBean(createReqVO, AdminKefuDO.class);
        adminKefuMapper.insert(adminKefu);
        // 返回
        return adminKefu.getId();
    }

    @Override
    public void updateAdminKefu(AdminKefuSaveReqVO updateReqVO) {
        // 校验存在
        validateAdminKefuExists(updateReqVO.getId());
        // 更新
        AdminKefuDO updateObj = BeanUtils.toBean(updateReqVO, AdminKefuDO.class);
        adminKefuMapper.updateById(updateObj);
    }

    @Override
    public void deleteAdminKefu(Long id) {
        // 校验存在
        validateAdminKefuExists(id);
        // 删除
        adminKefuMapper.deleteById(id);
    }

    private void validateAdminKefuExists(Long id) {
        if (adminKefuMapper.selectById(id) == null) {
            throw exception(ADMIN_KEFU_NOT_EXISTS);
        }
    }

    @Override
    public AdminKefuDO getAdminKefu(Long id) {
        return adminKefuMapper.selectById(id);
    }

    @Override
    public PageResult<AdminKefuDO> getAdminKefuPage(AdminKefuPageReqVO pageReqVO) {
        return adminKefuMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AdminKefuDO> getAdminKefuList() {
        return adminKefuMapper.selectList(new LambdaQueryWrapperX<AdminKefuDO>()
                .eq(AdminKefuDO::getStatus, true));
    }

}